<template>
  <Panel>
    <!-- 标题 -->
    <template #title>添加账号</template>
    <!-- 内容 -->
    <template
      ><el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="uname">
          <el-input
            type="text"
            v-model="ruleForm.uname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户组" prop="age">
          <template>
            <el-select v-model="ruleForm.age" placeholder="请选择">
              <el-option label="普通管理员" value="普通管理员"> </el-option>
              <el-option label="超级管理员" value="超级管理员"> </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form></template
    >
  </Panel>
</template>

<script>
// 引入
import { validatePass, validatePass2 } from '../../utils/valide.js'

// 引入解构发请求
import { addUser } from '../../api/user.js'

export default {
  data() {
    /*  var checkAge = (rule, value, callback) => {
      if (value === 0) {
        return callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    } */
    return {
      ruleForm: {
        uname: '',
        checkPass: '',
        age: ''
      },
      // 规则对象
      rules: {
        uname: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        /* age: [{ validator: checkAge, trigger: 'blur' }] */
        age: [{ requied: true, message: '请选择管理权限', trigger: 'change' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 85 data
          /* alert('submit!')  */
          const { code, msg } /* , msg */ = await addUser({
            account: this.ruleForm.uname,
            password: this.ruleForm.checkPass,
            userGroup: this.ruleForm.age
          })
          /* console.log(data) */
          if (code === 0) {
            this.$message({
              type: 'success',
              message: msg
            })
            setTimeout(() => {
              this.$router.push('/zhanghao/list')
            }, 1000)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
/* .title {
  font-weight: 700;
} */
.demo-ruleForm {
  width: 360px;
}
</style>
